// subPages/business/earnings/earnings.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'earnings'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/business/down.png',
    upImg: '/resources/business/up.png',
    date: '',
    isDate: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    loadMoreView = this.selectComponent("#loadMoreView")
    that.getEarnings()
    that.setData({
      dateImgSrc: this.data.downImg,
      goodsImgSrc: this.data.downImg,
      urlType: that.getUrl()
    })
    that.getData()
  },

  getUrl:function(){
    var urlType = ''
    switch (app.globalData.shopType) {
      case '0':
        urlType = 'get_all_sy_list';
        break;
      case '1':
        urlType = 'health_shop_get_all_sy_list';
        break;
      case '2':
        urlType = 'kitchen_get_all_sy_list';
        break;
      case '3':
        urlType = 'admin_community_get_all_sy_list';
        break;
    }
    return urlType
  },

  //获取收益
  getEarnings:function(){
    var that = this
    var url = ''
    var data = {}
    switch (app.globalData.shopType) {
      case '0':
        url = '/admin_community_service_shop_get_all_sy'
        data = {service_shop_id: app.globalData.shopId}
        break;
      case '1':
        url = '/admin_get_all_sy'
        data = {health_shop_id: app.globalData.shopId}        
        break;
      case '2':
        url = '/admin_community_service_kitchen_get_all_sy'
        data = {kitchen_id: app.globalData.shopId}
        break;
      case '3':
        url = '/admin_community_get_all_sy'
        data = {kitchen_id: app.globalData.shopId}
        break;
    }
    util.getAjax(`${app.globalData.apiUrl}${url}`,function(res){
      // console.log(res)
      that.setData({
        earning: res.data?res.data:0
      })
    },data)
  },

  //设置日期
  setDate:function(){
    let dateStart = util.setTime(new Date())
    let Y = dateStart.split('-')
    this.setData({
      dateStart: Y[0]- 3,
      dateEnd: dateStart,
    })
  },

  bindDateChange: function(e) {
    this.setData({
      isDate: true,
      date: e.detail.value,
      dateImgSrc: this.data.downImg
    })
    this.getData()
  },

  bindDateCancel:function(){
    this.setData({
      dateImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    if(that.data.dateImgSrc == that.data.downImg){
      that.setData({
        dateImgSrc: that.data.upImg
      })
    }
  },

  //删除标签
  delete:function(){
    this.setData({
      isDate: false,
      date: ''
    })
    this.getData()
  },

  //获取数据
  getData:function(){
    var that = this
    var start_time = '',end_time = ''
    if(that.data.date){
      start_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 00:00:00').getTime())/1000
      end_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 23:59:59').getTime())/1000
    }
    switch (app.globalData.shopType) {
      case '0':
        util.getAjax(`${app.globalData.apiUrl}/${that.data.urlType}`,that.getEarningsData,{service_shop_id: app.globalData.shopId,p: p,num: num,start_time: start_time,end_time: end_time},that.getEarningsErr)
        break;
      case '1':
        util.getAjax(`${app.globalData.apiUrl}/${that.data.urlType}`,that.getEarningsData,{health_shop_id: app.globalData.shopId,p: p,num: num,start_time: start_time,end_time: end_time},that.getEarningsErr)
        break;
      case '2':
        util.getAjax(`${app.globalData.apiUrl}/${that.data.urlType}`,that.getEarningsData,{kitchen_id: app.globalData.shopId,p: p,num: num,start_time: start_time,end_time: end_time},that.getEarningsErr)
        break;
      case '3':
        util.getAjax(`${app.globalData.apiUrl}/${that.data.urlType}`,that.getEarningsData,{community_id: app.globalData.shopId,p: p,num: num,start_time: start_time,end_time: end_time},that.getEarningsErr)
        break;
    }
  },

  getEarningsData(res){
    var that = this
    console.log(res)
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.earningsList.concat(res.data.list)
      }
      that.setData({
        earningsList: list
      })
    }else{
      that.setData({
        earningsList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  getEarningsErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取收益失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getEarningsData()
  },

  clickLoadMore: function(e) {
    this.getEarningsData()
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },


  //数据统计
  statistical:function(){
    wx.navigateTo({
      url: '../statistics/statistics'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getEarningsData()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})